package com.heima.behavior.service.impl;


import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 加载文章详情 数据回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {


        Map<String, Boolean> map = new HashMap<>();
        if (dto.getArticleId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "文章id不能为空");
        }
        //得到登录用户的id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));

        //根据登录云过户id 查询是否关注该作者
        Double score = redisTemplate.opsForZSet()
                .score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
        if (score != null) {
            map.put("isfollow", true);
        } else {
            map.put("isfollow", false);
        }


        //根据行为实体 文章id查询是否点赞
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (likesBehavior != null) {
            map.put("islike", true);
        } else {
            map.put("islike", false);
        }
        //根据行为实体 文章id查询是否不喜欢
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (unlikesBehavior != null) {
            map.put("isunlike", true);
        } else {
            map.put("isunlike", false);
        }
        //根据行为实体 文章id查询是否收藏
        ApCollection collection = mongoTemplate.findOne(query, ApCollection.class);
        if (collection != null) {
            map.put("iscollection", true);
        } else {
            map.put("iscollection", false);
        }


        return ResponseResult.okResult(map);
    }
}
